package cn.icare.cms.impl;

import cn.icare.cms.AreaService;
import cn.icare.cms.DateUtil;
import cn.icare.cms.ListUtil;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.AreaMapper;
import cn.icare.cms.dao.po.Area;
import cn.icare.cms.dao.po.Position;
import cn.icare.cms.model.ro.AddAreaRO;
import cn.icare.cms.model.ro.EditAreaRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import cn.icare.cms.model.vo.rest.PositionVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {

    @Resource
    private AreaMapper areaMapper;

    private final Logger logger = LoggerFactory.getLogger(AreaServiceImpl.class);


    @Override
    public Long createOne(AddAreaRO ro,String actionName) {

        String other="";

        Area area = new Area();
        area.setName(ro.getName());
        area.setCode(ro.getCode());
        area.setMemo(ro.getMemo());
        String currentDate= DateUtil.formateToDateTime(new Date(),"yyyy-MM-dd HH:mm:ss");
        other=String.format("%s 于%s 创建区域：%s", actionName,currentDate,area.getName());

        if (ro.getParentId() == null) {
            area.setParentid(0L);
        } else {
            area.setParentid(ro.getParentId());
        }
        if (ro.getLat() != null) {
            area.setLat(ro.getLat());
        } else {
            area.setLat(0D);
        }

        if (ro.getLon() != null) {
            area.setLon(ro.getLon());
        } else {
            area.setLon(0D);
        }
        if (ro.getMasterId() == null) {
            area.setMasterid(0L);
        } else {
            area.setMasterid(ro.getMasterId());
            area.setMasterstarttime(new Date());
            other+=String.format("(设置Id=%s的用户为团长)",ro.getMasterId());
        }
        area.setOther(other);
        area.setCreatetime(new Date());

        int result = this.areaMapper.insert(area);
        if (result > 0) {
            return area.getId();
        }
        return 0L;
    }

    @Override
    public boolean updateOne(EditAreaRO ro,String actionName) {
        Area area = new Area();
        area.setId(ro.getId());
        area.setName(ro.getName());
        area.setParentid(ro.getParentId());
        area.setCreatetime(new Date());
        area.setCode(ro.getCode());
        area.setMemo(ro.getMemo());
        String other="";
        String currentDate= DateUtil.formateToDateTime(new Date(),"yyyy-MM-dd HH:mm:ss");
        other=String.format("%s 于%s 修改区域：%s", actionName,currentDate,area.getName());

        if (ro.getParentId() == null) {
            area.setParentid(0L);
        } else {
            area.setParentid(ro.getParentId());
        }
        if (ro.getLat() != null) {
            area.setLat(ro.getLat());
        } else {
            area.setLat(0D);
        }
        if (ro.getLon() != null) {
            area.setLon(ro.getLon());
        } else {
            area.setLon(0D);
        }
        if (ro.getMasterId() !=null) {
            area.setMasterid(ro.getMasterId());
            area.setMasterstarttime(new Date());
        }
        else
        {
            area.setMasterid(0L);
            area.setMasterstarttime(null);
        }
        Area areaOld=this.areaMapper.selectByPrimaryKey(ro.getId());
        if(areaOld!=null)
        {
            if(!(areaOld.getMasterid()+"").equalsIgnoreCase(area.getMasterid()+""))
            {
                if(area.getMasterid()!=null && area.getMasterid()>0)
                {
                    other+=String.format(",(设置Id=%s的用户为团长).",ro.getMasterId());
                }
                else
                {
                    other+=String.format(",(取消Id=%s的用户的团长职务).",areaOld.getMasterid());
                }
            }
        }
        area.setOther(other);
        int result = this.areaMapper.updateByPrimaryKeySelective(area);
        return result > 0;
    }

    @Override
    public boolean deleteOne(Long id) {

        boolean result = false;
        try {
            result = this.areaMapper.deleteByPrimaryKey(id) > 0;
        } catch (Exception e) {
            this.logger.error("deleteOne 异常", e);
        }
        return result;

    }

    @Override
    public boolean deleteBatch(List<Long> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(Area.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.areaMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public Area getOne(Long id) {
        Area area = this.areaMapper.selectByPrimaryKey(id);
        return area;
    }

    @Override
    public MsgDatasVO getList(Long parentId, String name, Integer pageNum, Integer pageSize) {

        MsgDatasVO result = new MsgDatasVO();
        try {
            if (pageSize == 0) {
                Example example = new Example(Area.class);
                Example.Criteria criteria = example.createCriteria();
                if (StringUtil.isNotEmpty(name)) {
                    criteria.andLike("name", "%" + name + "%");
                }
                if (parentId > 0) {
                    criteria.andEqualTo("parentid", parentId);
                }
                List<Area> dataList = this.areaMapper.selectByExample(example);
                result.setTotal(dataList.size());
                result.setDatas(dataList);
            } else {
                PageHelper.startPage(pageNum, pageSize);
                Example example = new Example(Area.class);
                Example.Criteria criteria = example.createCriteria();
                if (StringUtil.isNotEmpty(name)) {
                    criteria.andLike("name", "%" + name + "%");
                }
                if (parentId > 0) {
                    criteria.andEqualTo("parentid", parentId);
                }
                List<Area> dataList = this.areaMapper.selectByExample(example);
                PageInfo pageInfo = new PageInfo(dataList);
                result.setTotal(pageInfo.getTotal());
                result.setDatas(dataList);
            }
        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }

    @Override
    public  Long getMasterId(String code) {

        Long masterId=0L;
        try {
            Example example = new Example(Area.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("code", code);
            Area area = this.areaMapper.selectOneByExample(example);
            if (area != null && area.getMasterid() != null && area.getMasterid() > 0) {
                masterId = area.getMasterid();
            }
        }
        catch (Exception ex)
        {
            this.logger.error("getMasterId 异常", ex);
        }
        return masterId;
    }
}
